{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> start QUANTAXIS\n",
      "QUANTAXIS>> Welcome to QUANTAXIS, the Version is 1.1.0\n",
      "QUANTAXIS>>  \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``########`````##````````##``````````##`````````####````````##```##########````````#``````##``````###```##`````######`` \n",
      "  `##``````## ```##````````##`````````####````````##`##```````##```````##```````````###``````##````##`````##```##`````##` \n",
      "  ##````````##```##````````##````````##`##````````##``##``````##```````##``````````####```````#```##``````##```##``````## \n",
      "  ##````````##```##````````##```````##```##```````##```##`````##```````##`````````##`##```````##`##```````##````##``````` \n",
      "  ##````````##```##````````##``````##`````##``````##````##````##```````##````````##``###```````###````````##`````##`````` \n",
      "  ##````````##```##````````##``````##``````##`````##`````##```##```````##```````##````##```````###````````##``````###```` \n",
      "  ##````````##```##````````##`````##````````##````##``````##``##```````##``````##``````##`````##`##```````##````````##``` \n",
      "  ##````````##```##````````##````#############````##```````##`##```````##`````###########`````##``##``````##`````````##`` \n",
      "  ###```````##```##````````##```##```````````##```##```````##`##```````##````##`````````##```##```##``````##```##`````##` \n",
      "  `##``````###````##``````###``##`````````````##``##````````####```````##```##``````````##``###````##`````##````##`````## \n",
      "  ``#########``````########```##``````````````###`##``````````##```````##``##````````````##`##``````##````##`````###``### \n",
      "  ````````#####`````````````````````````````````````````````````````````````````````````````````````````````````````##``  \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``````````````````````````Copyright``yutiansut``2018``````QUANTITATIVE FINANCIAL FRAMEWORK````````````````````````````` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# utf-8\n",
    "import QUANTAXIS as QA\n",
    "\n",
    "market = QA.QA_Market(if_start_orderthreading=True)\n",
    "portfolio= QA.QA_Portfolio()\n",
    "# 创建两个account\n",
    "# 这里是创建一个资产组合,然后在组合里面创建两个account  你可以想象成股票里面的两个策略账户\n",
    "# 然后返回的是这个账户的id\n",
    "a_1 = portfolio.new_account()\n",
    "a_1.reset_assets(100000000)\n",
    "a_1.frequence = QA.FREQUENCE.ONE_MIN\n",
    "market.start()\n",
    "\n",
    "market.connect(QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<_MainThread(MainThread, started 14868)>,\n",
       " <Thread(Thread-4, started daemon 3456)>,\n",
       " <Heartbeat(Thread-5, started daemon 10936)>,\n",
       " <HistorySavingThread(IPythonHistorySavingThread, started 21104)>,\n",
       " <ParentPollerWindows(Thread-3, started daemon 18260)>,\n",
       " <Thread(pymongo_server_monitor_thread, started daemon 21040)>,\n",
       " <Thread(pymongo_kill_cursors_thread, started daemon 21484)>,\n",
       " <Thread(pymongo_kill_cursors_thread, started daemon 7180)>,\n",
       "  <QA_ENGINE with ['ORDER', 'backtest'] kernels ident 5756>,\n",
       " <QA_ThreadORDER  id=2440348498744 ident 3820>,\n",
       " <QA_Threadbacktest  id=2440427795120 ident 5404>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "threading.enumerate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sub\n",
      "subscribe\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{< QA_Account Acc_D17B8LaU>: < QA_Broker backtest thread 14868 >}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.login(QA.BROKER_TYPE.BACKETEST, a_1.account_cookie, a_1)\n",
    "\n",
    "market.order_handler.monitor\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "market._sync_orders()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{< QA_Account Acc_D17B8LaU>: < QA_Broker backtest thread 14868 >}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{a_1:market.broker[a_1.broker]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> MARKET WARING: SOMEING WRONG WITH ORDER \n",
      " \n",
      "QUANTAXIS>> code 000001 date 2018-08-14 14:58:00 price 0 order_model MARKET amount_model by_amount\n",
      "QUANTAXIS>> MARKET WARING: SOMEING WRONG WITH ORDER \n",
      " \n",
      "QUANTAXIS>> code 000004 date 2018-08-14 14:58:00 price 0 order_model MARKET amount_model by_amount\n"
     ]
    }
   ],
   "source": [
    "for code in ['000001', '000002', '000004', '600010', '000007','600000']:\n",
    "    market.insert_order(a_1.account_cookie,code=code,\n",
    "                            price=0,\n",
    "                            amount=1000,\n",
    "                            time='2018-08-14 14:58:00',\n",
    "                            towards=QA.ORDER_DIRECTION.BUY,\n",
    "                            order_model=QA.ORDER_MODEL.MARKET,\n",
    "                            amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,\n",
    "                            market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                            frequence=QA.FREQUENCE.ONE_MIN,\n",
    "                            broker_name=QA.BROKER_TYPE.BACKETEST,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{< QA_Account Acc_D17B8LaU>: < QA_Broker backtest thread 14868 >}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Acc_D17B8LaU', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '000002', None, 1, 22.33, 22.33, 'trade_success', 1000, 1000, 22335.5825, 0, 'Order_Yq0d97zl', 'Trade_071jybh4']\n"
     ]
    }
   ],
   "source": [
    "market.order_handler.monitor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on_insert_order\n",
      "< QA_Order realorder_id Order_Yq0d97zl datetime:2018-08-14 14:58:00 code:000002 amount:1000 price:22.33 towards:1 btype:stock_cn order_id:Order_Yq0d97zl account:Acc_D17B8LaU status:queued >\n",
      "queued\n",
      "['Acc_D17B8LaU', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '600010', None, 1, 1.63, 1.63, 'trade_success', 1000, 1000, 1635.0, 0, 'Order_fZ1jlwvh', 'Trade_dFUjKIa6']\n",
      "on_insert_order"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>order_time</th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_price</th>\n",
       "      <th>order_price</th>\n",
       "      <th>status</th>\n",
       "      <th>order_amount</th>\n",
       "      <th>trade_amount</th>\n",
       "      <th>cancel_amount</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>account_cookie</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Acc_D17B8LaU</th>\n",
       "      <th>Order_Yq0d97zl</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>22.33</td>\n",
       "      <td>22.33</td>\n",
       "      <td>trade_success</td>\n",
       "      <td>1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        order_time    code  name  towards  \\\n",
       "account_cookie realorder_id                                                 \n",
       "Acc_D17B8LaU   Order_Yq0d97zl  2018-08-14 14:58:00  000002  None        1   \n",
       "\n",
       "                               trade_price  order_price         status  \\\n",
       "account_cookie realorder_id                                              \n",
       "Acc_D17B8LaU   Order_Yq0d97zl        22.33        22.33  trade_success   \n",
       "\n",
       "                               order_amount  trade_amount  cancel_amount  \n",
       "account_cookie realorder_id                                               \n",
       "Acc_D17B8LaU   Order_Yq0d97zl          1000          1000              0  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "< QA_Order realorder_id Order_fZ1jlwvh datetime:2018-08-14 14:58:00 code:600010 amount:1000 price:1.63 towards:1 btype:stock_cn order_id:Order_fZ1jlwvh account:Acc_D17B8LaU status:queued >\n",
      "queued\n"
     ]
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].query_orders(a_1.account_cookie,'')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>trade_time</th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_amount</th>\n",
       "      <th>trade_price</th>\n",
       "      <th>trade_money</th>\n",
       "      <th>trade_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>account_cookie</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Acc_D17B8LaU</th>\n",
       "      <th>Order_Yq0d97zl</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>1000</td>\n",
       "      <td>22.33</td>\n",
       "      <td>22335.5825</td>\n",
       "      <td>Trade_5xOTrtgk</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Order_fZ1jlwvh</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600010</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>1000</td>\n",
       "      <td>1.63</td>\n",
       "      <td>1635.0000</td>\n",
       "      <td>Trade_SZmaBrQ6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        trade_time    code  name  towards  \\\n",
       "account_cookie realorder_id                                                 \n",
       "Acc_D17B8LaU   Order_Yq0d97zl  2018-08-14 14:58:00  000002  None        1   \n",
       "               Order_fZ1jlwvh  2018-08-14 14:58:00  600010  None        1   \n",
       "\n",
       "                               trade_amount  trade_price  trade_money  \\\n",
       "account_cookie realorder_id                                             \n",
       "Acc_D17B8LaU   Order_Yq0d97zl          1000        22.33   22335.5825   \n",
       "               Order_fZ1jlwvh          1000         1.63    1635.0000   \n",
       "\n",
       "                                     trade_id  \n",
       "account_cookie realorder_id                    \n",
       "Acc_D17B8LaU   Order_Yq0d97zl  Trade_5xOTrtgk  \n",
       "               Order_fZ1jlwvh  Trade_SZmaBrQ6  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n",
      "['Acc_D17B8LaU', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '000007', None, 1, 0.0, 13.23, 'trade_price_limit', 1000, 0, 5, 0, 'Order_ANbWnkxh', 'Trade_D9uCndMJ']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_ANbWnkxh datetime:2018-08-14 14:58:00 code:000007 amount:1000 price:13.23 towards:1 btype:stock_cn order_id:Order_ANbWnkxh account:Acc_D17B8LaU status:queued >\n",
      "queued\n",
      "['Acc_D17B8LaU', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '600000', None, 1, 10.07, 10.065000000000001, 'trade_success', 1000, 1000, 10070.000000000002, 0, 'Order_Z9mQWKPE', 'Trade_aGbiP8dD']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_Z9mQWKPE datetime:2018-08-14 14:58:00 code:600000 amount:1000 price:10.065000000000001 towards:1 btype:stock_cn order_id:Order_Z9mQWKPE account:Acc_D17B8LaU status:queued >\n",
      "queued\n"
     ]
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].query_orders(a_1.account_cookie,'filled')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>22.33</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997763e+07</td>\n",
       "      <td>Order_Yq0d97zl</td>\n",
       "      <td>Order_Yq0d97zl</td>\n",
       "      <td>Trade_5xOTrtgk</td>\n",
       "      <td>Acc_D17B8LaU</td>\n",
       "      <td>5.5825</td>\n",
       "      <td>33.495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600010</td>\n",
       "      <td>1.63</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997600e+07</td>\n",
       "      <td>Order_fZ1jlwvh</td>\n",
       "      <td>Order_fZ1jlwvh</td>\n",
       "      <td>Trade_SZmaBrQ6</td>\n",
       "      <td>Acc_D17B8LaU</td>\n",
       "      <td>0.4075</td>\n",
       "      <td>2.445</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount          cash        order_id  \\\n",
       "0  2018-08-14 14:58:00  000002  22.33    1000  9.997763e+07  Order_Yq0d97zl   \n",
       "1  2018-08-14 14:58:00  600010   1.63    1000  9.997600e+07  Order_fZ1jlwvh   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission     tax  \n",
       "0  Order_Yq0d97zl  Trade_5xOTrtgk   Acc_D17B8LaU      5.5825  33.495  \n",
       "1  Order_fZ1jlwvh  Trade_SZmaBrQ6   Acc_D17B8LaU      0.4075   2.445  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n"
     ]
    }
   ],
   "source": [
    "a_1.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sub\n",
      "subscribe\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2 = portfolio.new_account()\n",
    "a_2.reset_assets(100000000)\n",
    "a_2.frequence = QA.FREQUENCE.ONE_MIN\n",
    "\n",
    "market.login(QA.BROKER_TYPE.BACKETEST, a_2.account_cookie, a_2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> MARKET WARING: SOMEING WRONG WITH ORDER \n",
      " \n",
      "QUANTAXIS>> code 000001 date 2018-08-14 14:58:00 price 0 order_model MARKET amount_model by_amount\n",
      "QUANTAXIS>> MARKET WARING: SOMEING WRONG WITH ORDER \n",
      " \n",
      "QUANTAXIS>> code 000004 date 2018-08-14 14:58:00 price 0 order_model MARKET amount_model by_amount\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Acc_xvgUlRXT', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '000002', None, 1, 22.33, 22.33, 'trade_success', 1000, 1000, 22335.5825, 0, 'Order_hGuTR8mx', 'Trade_y6kfTWgr']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_hGuTR8mx datetime:2018-08-14 14:58:00 code:000002 amount:1000 price:22.33 towards:1 btype:stock_cn order_id:Order_hGuTR8mx account:Acc_xvgUlRXT status:queued >\n",
      "queued\n",
      "['Acc_xvgUlRXT', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '600010', None, 1, 1.63, 1.63, 'trade_success', 1000, 1000, 1635.0, 0, 'Order_FMQ7dCiD', 'Trade_qT2fh7pU']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_FMQ7dCiD datetime:2018-08-14 14:58:00 code:600010 amount:1000 price:1.63 towards:1 btype:stock_cn order_id:Order_FMQ7dCiD account:Acc_xvgUlRXT status:queued >\n",
      "queued\n",
      "['Acc_xvgUlRXT', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '000007', None, 1, 0.0, 13.23, 'trade_price_limit', 1000, 0, 5, 0, 'Order_Ie3pbwJY', 'Trade_WTMjypSm']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_Ie3pbwJY datetime:2018-08-14 14:58:00 code:000007 amount:1000 price:13.23 towards:1 btype:stock_cn order_id:Order_Ie3pbwJY account:Acc_xvgUlRXT status:queued >\n",
      "queued\n",
      "['Acc_xvgUlRXT', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '600000', None, 1, 10.07, 10.065000000000001, 'trade_success', 1000, 1000, 10070.000000000002, 0, 'Order_pu6iwRSr', 'Trade_gCdsP3Hu']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_pu6iwRSr datetime:2018-08-14 14:58:00 code:600000 amount:1000 price:10.065000000000001 towards:1 btype:stock_cn order_id:Order_pu6iwRSr account:Acc_xvgUlRXT status:queued >\n",
      "queued\n",
      "receive deal\n",
      "receive deal\n",
      "receive deal\n"
     ]
    }
   ],
   "source": [
    "for code in ['000001', '000002', '000004', '600010', '000007','600000']:\n",
    "    market.insert_order(a_2.account_cookie,code=code,\n",
    "                            price=0,\n",
    "                            amount=1000,\n",
    "                            time='2018-08-14 14:58:00',\n",
    "                            towards=QA.ORDER_DIRECTION.BUY,\n",
    "                            order_model=QA.ORDER_MODEL.MARKET,\n",
    "                            amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,\n",
    "                            market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                            frequence=QA.FREQUENCE.ONE_MIN,\n",
    "                            broker_name=QA.BROKER_TYPE.BACKETEST,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>22.33</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997763e+07</td>\n",
       "      <td>Order_hGuTR8mx</td>\n",
       "      <td>Order_hGuTR8mx</td>\n",
       "      <td>Trade_FpBN8bwM</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "      <td>5.5825</td>\n",
       "      <td>33.495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600010</td>\n",
       "      <td>1.63</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997600e+07</td>\n",
       "      <td>Order_FMQ7dCiD</td>\n",
       "      <td>Order_FMQ7dCiD</td>\n",
       "      <td>Trade_tTEMKASC</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "      <td>0.4075</td>\n",
       "      <td>2.445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600000</td>\n",
       "      <td>10.07</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.996591e+07</td>\n",
       "      <td>Order_pu6iwRSr</td>\n",
       "      <td>Order_pu6iwRSr</td>\n",
       "      <td>Trade_OGT1SE9x</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "      <td>2.5175</td>\n",
       "      <td>15.105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount          cash        order_id  \\\n",
       "0  2018-08-14 14:58:00  000002  22.33    1000  9.997763e+07  Order_hGuTR8mx   \n",
       "1  2018-08-14 14:58:00  600010   1.63    1000  9.997600e+07  Order_FMQ7dCiD   \n",
       "2  2018-08-14 14:58:00  600000  10.07    1000  9.996591e+07  Order_pu6iwRSr   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission     tax  \n",
       "0  Order_hGuTR8mx  Trade_FpBN8bwM   Acc_xvgUlRXT      5.5825  33.495  \n",
       "1  Order_FMQ7dCiD  Trade_tTEMKASC   Acc_xvgUlRXT      0.4075   2.445  \n",
       "2  Order_pu6iwRSr  Trade_OGT1SE9x   Acc_xvgUlRXT      2.5175  15.105  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>account_cookie</th>\n",
       "      <th>Acc_D17B8LaU</th>\n",
       "      <th>Acc_xvgUlRXT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>account_cookie</th>\n",
       "      <td>Acc_D17B8LaU</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allow_sellopen</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>allow_t0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>broker</th>\n",
       "      <td>backtest</td>\n",
       "      <td>backtest</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cash</th>\n",
       "      <td>[100000000, 99977630.9225, 99975998.07, 999659...</td>\n",
       "      <td>[100000000, 99977630.9225, 99975998.07, 999659...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>commission_coeff</th>\n",
       "      <td>0.00025</td>\n",
       "      <td>0.00025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>current_time</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>history</th>\n",
       "      <td>[[2018-08-14 14:58:00, 000002, 22.33, 1000, 99...</td>\n",
       "      <td>[[2018-08-14 14:58:00, 000002, 22.33, 1000, 99...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>init_assets</th>\n",
       "      <td>{'cash': 100000000, 'hold': {}}</td>\n",
       "      <td>{'cash': 100000000, 'hold': {}}</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>margin_level</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>market_type</th>\n",
       "      <td>stock_cn</td>\n",
       "      <td>stock_cn</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>portfolio_cookie</th>\n",
       "      <td>Portfolio_5miNbPan</td>\n",
       "      <td>Portfolio_5miNbPan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>quantaxis_version</th>\n",
       "      <td>1.1.0</td>\n",
       "      <td>1.1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>running_environment</th>\n",
       "      <td>backtest</td>\n",
       "      <td>backtest</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>running_time</th>\n",
       "      <td>2018-08-18 21:40:04.689366</td>\n",
       "      <td>2018-08-18 21:41:09.733599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>source</th>\n",
       "      <td>account</td>\n",
       "      <td>account</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>strategy_name</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tax_coeff</th>\n",
       "      <td>0.0015</td>\n",
       "      <td>0.0015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_index</th>\n",
       "      <td>[2018-08-14 14:58:00, 2018-08-14 14:58:00, 201...</td>\n",
       "      <td>[2018-08-14 14:58:00, 2018-08-14 14:58:00, 201...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_cookie</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "account_cookie                                            Acc_D17B8LaU  \\\n",
       "account_cookie                                            Acc_D17B8LaU   \n",
       "allow_sellopen                                                   False   \n",
       "allow_t0                                                         False   \n",
       "broker                                                        backtest   \n",
       "cash                 [100000000, 99977630.9225, 99975998.07, 999659...   \n",
       "commission_coeff                                               0.00025   \n",
       "current_time                                                      None   \n",
       "history              [[2018-08-14 14:58:00, 000002, 22.33, 1000, 99...   \n",
       "init_assets                            {'cash': 100000000, 'hold': {}}   \n",
       "margin_level                                                     False   \n",
       "market_type                                                   stock_cn   \n",
       "portfolio_cookie                                    Portfolio_5miNbPan   \n",
       "quantaxis_version                                                1.1.0   \n",
       "running_environment                                           backtest   \n",
       "running_time                                2018-08-18 21:40:04.689366   \n",
       "source                                                         account   \n",
       "strategy_name                                                     None   \n",
       "tax_coeff                                                       0.0015   \n",
       "trade_index          [2018-08-14 14:58:00, 2018-08-14 14:58:00, 201...   \n",
       "user_cookie                                                       None   \n",
       "\n",
       "account_cookie                                            Acc_xvgUlRXT  \n",
       "account_cookie                                            Acc_xvgUlRXT  \n",
       "allow_sellopen                                                   False  \n",
       "allow_t0                                                         False  \n",
       "broker                                                        backtest  \n",
       "cash                 [100000000, 99977630.9225, 99975998.07, 999659...  \n",
       "commission_coeff                                               0.00025  \n",
       "current_time                                                      None  \n",
       "history              [[2018-08-14 14:58:00, 000002, 22.33, 1000, 99...  \n",
       "init_assets                            {'cash': 100000000, 'hold': {}}  \n",
       "margin_level                                                     False  \n",
       "market_type                                                   stock_cn  \n",
       "portfolio_cookie                                    Portfolio_5miNbPan  \n",
       "quantaxis_version                                                1.1.0  \n",
       "running_environment                                           backtest  \n",
       "running_time                                2018-08-18 21:41:09.733599  \n",
       "source                                                         account  \n",
       "strategy_name                                                     None  \n",
       "tax_coeff                                                       0.0015  \n",
       "trade_index          [2018-08-14 14:58:00, 2018-08-14 14:58:00, 201...  \n",
       "user_cookie                                                       None  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "portfolio.table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>22.33</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997763e+07</td>\n",
       "      <td>Order_Yq0d97zl</td>\n",
       "      <td>Order_Yq0d97zl</td>\n",
       "      <td>Trade_5xOTrtgk</td>\n",
       "      <td>Acc_D17B8LaU</td>\n",
       "      <td>5.5825</td>\n",
       "      <td>33.495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600010</td>\n",
       "      <td>1.63</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997600e+07</td>\n",
       "      <td>Order_fZ1jlwvh</td>\n",
       "      <td>Order_fZ1jlwvh</td>\n",
       "      <td>Trade_SZmaBrQ6</td>\n",
       "      <td>Acc_D17B8LaU</td>\n",
       "      <td>0.4075</td>\n",
       "      <td>2.445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600000</td>\n",
       "      <td>10.07</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.996591e+07</td>\n",
       "      <td>Order_Z9mQWKPE</td>\n",
       "      <td>Order_Z9mQWKPE</td>\n",
       "      <td>Trade_J7BGSm5V</td>\n",
       "      <td>Acc_D17B8LaU</td>\n",
       "      <td>2.5175</td>\n",
       "      <td>15.105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>22.33</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997763e+07</td>\n",
       "      <td>Order_hGuTR8mx</td>\n",
       "      <td>Order_hGuTR8mx</td>\n",
       "      <td>Trade_FpBN8bwM</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "      <td>5.5825</td>\n",
       "      <td>33.495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600010</td>\n",
       "      <td>1.63</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997600e+07</td>\n",
       "      <td>Order_FMQ7dCiD</td>\n",
       "      <td>Order_FMQ7dCiD</td>\n",
       "      <td>Trade_tTEMKASC</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "      <td>0.4075</td>\n",
       "      <td>2.445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600000</td>\n",
       "      <td>10.07</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.996591e+07</td>\n",
       "      <td>Order_pu6iwRSr</td>\n",
       "      <td>Order_pu6iwRSr</td>\n",
       "      <td>Trade_OGT1SE9x</td>\n",
       "      <td>Acc_xvgUlRXT</td>\n",
       "      <td>2.5175</td>\n",
       "      <td>15.105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount          cash        order_id  \\\n",
       "0  2018-08-14 14:58:00  000002  22.33    1000  9.997763e+07  Order_Yq0d97zl   \n",
       "1  2018-08-14 14:58:00  600010   1.63    1000  9.997600e+07  Order_fZ1jlwvh   \n",
       "2  2018-08-14 14:58:00  600000  10.07    1000  9.996591e+07  Order_Z9mQWKPE   \n",
       "0  2018-08-14 14:58:00  000002  22.33    1000  9.997763e+07  Order_hGuTR8mx   \n",
       "1  2018-08-14 14:58:00  600010   1.63    1000  9.997600e+07  Order_FMQ7dCiD   \n",
       "2  2018-08-14 14:58:00  600000  10.07    1000  9.996591e+07  Order_pu6iwRSr   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission     tax  \n",
       "0  Order_Yq0d97zl  Trade_5xOTrtgk   Acc_D17B8LaU      5.5825  33.495  \n",
       "1  Order_fZ1jlwvh  Trade_SZmaBrQ6   Acc_D17B8LaU      0.4075   2.445  \n",
       "2  Order_Z9mQWKPE  Trade_J7BGSm5V   Acc_D17B8LaU      2.5175  15.105  \n",
       "0  Order_hGuTR8mx  Trade_FpBN8bwM   Acc_xvgUlRXT      5.5825  33.495  \n",
       "1  Order_FMQ7dCiD  Trade_tTEMKASC   Acc_xvgUlRXT      0.4075   2.445  \n",
       "2  Order_pu6iwRSr  Trade_OGT1SE9x   Acc_xvgUlRXT      2.5175  15.105  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "portfolio.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Order_Yq0d97zl': < QA_Order realorder_id Order_Yq0d97zl datetime:2018-08-14 14:58:00 code:000002 amount:1000 price:22.33 towards:1 btype:stock_cn order_id:Order_Yq0d97zl account:Acc_D17B8LaU status:success_all >,\n",
       " 'Order_fZ1jlwvh': < QA_Order realorder_id Order_fZ1jlwvh datetime:2018-08-14 14:58:00 code:600010 amount:1000 price:1.63 towards:1 btype:stock_cn order_id:Order_fZ1jlwvh account:Acc_D17B8LaU status:success_all >,\n",
       " 'Order_ANbWnkxh': < QA_Order realorder_id Order_ANbWnkxh datetime:2018-08-14 14:58:00 code:000007 amount:1000 price:13.23 towards:1 btype:stock_cn order_id:Order_ANbWnkxh account:Acc_D17B8LaU status:queued >,\n",
       " 'Order_Z9mQWKPE': < QA_Order realorder_id Order_Z9mQWKPE datetime:2018-08-14 14:58:00 code:600000 amount:1000 price:10.065000000000001 towards:1 btype:stock_cn order_id:Order_Z9mQWKPE account:Acc_D17B8LaU status:success_all >}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1.orders.order_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Order_hGuTR8mx': < QA_Order realorder_id Order_hGuTR8mx datetime:2018-08-14 14:58:00 code:000002 amount:1000 price:22.33 towards:1 btype:stock_cn order_id:Order_hGuTR8mx account:Acc_xvgUlRXT status:success_all >,\n",
       " 'Order_FMQ7dCiD': < QA_Order realorder_id Order_FMQ7dCiD datetime:2018-08-14 14:58:00 code:600010 amount:1000 price:1.63 towards:1 btype:stock_cn order_id:Order_FMQ7dCiD account:Acc_xvgUlRXT status:success_all >,\n",
       " 'Order_Ie3pbwJY': < QA_Order realorder_id Order_Ie3pbwJY datetime:2018-08-14 14:58:00 code:000007 amount:1000 price:13.23 towards:1 btype:stock_cn order_id:Order_Ie3pbwJY account:Acc_xvgUlRXT status:queued >,\n",
       " 'Order_pu6iwRSr': < QA_Order realorder_id Order_pu6iwRSr datetime:2018-08-14 14:58:00 code:600000 amount:1000 price:10.065000000000001 towards:1 btype:stock_cn order_id:Order_pu6iwRSr account:Acc_xvgUlRXT status:success_all >}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2.orders.order_list"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
